package com.oa.ylms.apps.dao.archives.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.archives.IMailInfoDao;
import com.oa.ylms.apps.entity.archives.MailInfoValue;

public class MailInfoDaoImpl extends HibernateDaoSupport implements
		IMailInfoDao {
	List<MailInfoValue> list;

	@Override
	public int addMailInfo(MailInfoValue mailInfo) throws Exception {
		if(mailInfo != null){
			this.getHibernateTemplate().save(mailInfo);
			return mailInfo.getMailId();
		}
		return 0;
	}

	@Override
	public boolean editMailInfo(MailInfoValue mailInfo) throws Exception {
		if(mailInfo != null){
			try{
				this.getHibernateTemplate().update(mailInfo);
			}catch(Exception e){
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public MailInfoValue queryByMailId(MailInfoValue mailInfo) throws Exception {
		if(mailInfo != null){
			return (MailInfoValue) this.getHibernateTemplate().get(MailInfoValue.class, mailInfo.getMailId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MailInfoValue> queryMailInfo(final MailInfoValue mailInfo) throws Exception {
		if(mailInfo != null){
			final String hql = "FROM MailInfoValue miv WHERE miv.mailId =? AND miv.verifyCode =?";
			list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					query.setInteger(0, mailInfo.getMailId());
					query.setString(1, mailInfo.getVerifyCode());
					list = query.list();
					return list;
				}
			});
			return list;
		}
		return null;
	}
	
	public List<MailInfoValue> getList() {
		return list;
	}

	public void setList(List<MailInfoValue> list) {
		this.list = list;
	}

}
